limsSampleHandle.detailPage = function (pageType) {
    return {

        getBtnToolbar: function (id, step) {
            var _this = this
            var buttons = [{
                type: 'button',
                text: 'GIKAM.BUTTON.BACK',
                icon: 'back',
                onClick: function () {
                    workspace.window.goBack()
                }
            },]
            if (step) {
                if (step === 'edit') {
                    buttons.push({
                        type: 'button',
                        text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                        icon: 'submit', color: 'success',
                        onClick: function () {
                            _this.submitSingle(id)
                        }
                    })
                } else if (step === 'audit') {
                    buttons.push({
                        type: 'button',
                        text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                        icon: 'pass',
                        onClick: function () {
                            _this.passSingle(id)
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                        icon: 'reject',
                        onClick: function () {
                            _this.rejectSingle(id)
                        }
                    })
                    if (pageType === 'back') {
                        buttons.push({
                            type: 'button',
                            text: 'GIKAM.BUTTON.COURIER.EDIT',
                            icon: 'edit',
                            onClick: function () {
                                _this.fillInLogisticsSingle(id, step)
                            }
                        })
                    }
                } else {

                }
            }
            return {
                type: 'btnToolbar',
                items: buttons
            }
        },

        submitSingle: function (id) {
            var _this = this
            Gikam.create('workflow').submit({
                data: [{id: id}],
                pageObject: limsSampleHandle,
                formatterToolbar: (toolbar, type, formId) => {
                    toolbar.forEach(b => {
                        if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                            b.class = 'blue'
                            b.icon = 'save'
                            b.color = 'white'
                        } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                            b.icon = 'back'
                        }
                    })
                    return toolbar
                }
            }).done(function () {
                workspace.window.goBack()
            })
        },

        passSingle: function (id) {
            var _this = this
            if (pageType === 'enter') {
                if (Gikam.isEmpty(id)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                    return
                }
                var checkLocationsUrl = limsSampleHandle.baseUrl + '/check-samplelocations'
                Gikam.postText(checkLocationsUrl, Gikam.getJsonWrapper(null, [null, [{id: id}]])).done(function (req) {
                    req = JSON.parse(req)
                    if (!req.message) {
                        _this.pass(id)
                    } else {
                        Gikam.confirm('GIKAM.BUTTON.CONFIRM', req.message + Gikam.propI18N('GIKAM.TIP.SAMPLE_NOT_SETLOCATIONS.CONFIRM'), function () {
                            _this.pass(id)
                        })
                    }
                })
            } else {
                _this.pass(id)
            }
        },

        pass: function (id) {
            Gikam.create('workflow').pass({
                data: [{id: id}],
                pageObject: limsSampleHandle,
                formatterToolbar: (toolbar, type, formId) => {
                    toolbar.forEach(b => {
                        if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                            b.class = 'blue'
                            b.icon = 'save'
                            b.color = 'white'
                        } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                            b.icon = 'back'
                        }
                    })
                    return toolbar
                }
            }).done(function () {
                workspace.window.goBack()
            })
        },

        rejectSingle: function (id) {
            var _this = this
            Gikam.create('workflow').reject({
                data: [{id: id}],
                pageObject: limsSampleHandle,
                formatterToolbar: (toolbar, type, formId) => {
                    toolbar.forEach(b => {
                        if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                            b.class = 'blue'
                            b.icon = 'save'
                            b.color = 'white'
                        } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                            b.icon = 'back'
                        }
                    })
                    return toolbar
                }
            }).done(function () {
                workspace.window.goBack()
            })
        },

        fillInLogisticsSingle: function (id, step) {
            var _this = this
            if (Gikam.isEmpty(id)) {
                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                return
            }
            Gikam.create('modal', {
                title: 'GIKAM.BUTTON.COURIER.EDIT',
                width: 400,
                height: 200,
                onAfterClose: function (data) {
                    if (Gikam.isNotEmpty(data)) {
                        workspace.window.showMask()
                        var chooseData = [{
                            id: id,
                            courierNumber: data.courierNumber,
                            courierCompany: data.courierCompany
                        }]
                        Gikam.put(limsSampleHandle.baseUrl + '/instant', JSON.stringify({
                            b: ['limsSampleHandleServiceImpl', chooseData]
                        })).done(function (data) {
                        }).always(function () {
                            workspace.window.closeMask()
                        })
                    }
                }
            })
            Gikam.create('layout', {
                renderTo: Gikam.getLastModal().window.$dom,
                center: {
                    items: [{
                        type: 'form',
                        id: _this.idManage(step).packFormId,
                        columns: 1,
                        titleWidth: 140,
                        fields: [{
                            field: 'courierNumber',
                            title: 'T_LIMS_SAMPLE_HANDLE.COURIERNUMBER',
                            validators: ['notEmpty'],
                        }, {
                            field: 'courierCompany',
                            title: 'T_LIMS_SAMPLE_HANDLE.COURIERCOMPANY',
                            validators: ['notEmpty'],
                            firstBlank: false,
                            type: 'select',
                            category: 'sys',
                            param: {
                                codeCategoryId: 'courierCompany'
                            }
                        }],
                    }, {
                        type: 'btnToolbar',
                        items: [{
                            type: 'button',
                            text: 'GIKAM.BUTTON.CONFIRM',
                            class: 'blue',
                            icon: 'save',
                            color: 'white',
                            onClick: function () {
                                //校验判断
                                var form = Gikam.getComp(_this.idManage(step).packFormId)
                                if (!form.validate()) {
                                    return
                                }
                                Gikam.getLastModal().close(form.getData())
                            }
                        }, {
                            type: 'button',
                            text: 'GIKAM.BUTTON.CANCEL',
                            icon: 'back',
                            onClick: function () {
                                Gikam.getLastModal().close()
                            }
                        }]
                    }]
                }
            })
        },

        getSampleHandleInfo: function (id, step) {
            var _this = this
            var toolBar = [], columns = limsSampleHandle.getSampleListColumns(false, pageType), editor = false
            if (step === 'edit') {
                toolBar.push({
                    type: 'button',
                    text: 'GIKAM.BUTTON.INSERT',
                    icon: 'add',
                    onClick: function () {
                        if (Gikam.isEmpty(id)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                            return
                        }
                        // 出库、销毁、返样，必须是已入库的样本
                        var param = {
                            inHandle_EQ: '1',
                            handleType_EQ: 'enterDone'
                        }
                        if (pageType === 'enter') {
                            // 未入库或出库的样本才能申请入库
                            param = {
                                inHandle_EQ: '0'
                            }
                        }
                        param.orgFilter = '1'
                        Gikam.create('modal', {
                            title: Gikam.choose.getConfig("sample-choose").title,
                            url: IFM_CONTEXT + Gikam.choose.getConfig("sample-choose").url + Gikam.param(param),
                            onAfterClose: function (rows) {
                                if (Gikam.isEmpty(rows)) {
                                    return
                                }
                                var chooseData = rows.map(function (item) {
                                    return {
                                        id: item.id,
                                        sampleHandleId: id,
                                        inHandle: '1',
                                        handleType: pageType
                                    }
                                })
                                Gikam.getComp(_this.idManage(step).sampleGridId).insert(limsSampleHandle.baseUrl + '/sample/add', chooseData).done(function () {
                                    Gikam.getComp(_this.idManage(step).sampleGridId).refresh()
                                })
                            }
                        })
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.BUTTON.REMOVE',
                    icon: 'remove-row',
                    onClick: function () {
                        let selections = Gikam.getComp(_this.idManage(step).sampleGridId).getSelections()
                        if (Gikam.isEmpty(selections)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                            return
                        }
                        Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'GIKAM.CONFIRM.REMOVE', function () {
                            workspace.window.showMask()
                            Gikam.postText(limsSampleHandle.baseUrl + '/sample/delete',
                                Gikam.getJsonWrapper(null, [null, selections])).done(function () {
                                Gikam.getComp(_this.idManage(step).sampleGridId).refresh()
                            }).always(function () {
                                workspace.window.closeMask()
                            })
                        })
                    }
                })
            } else if (step === 'audit') {
                if (pageType === 'enter') {// 入库
                    editor = true
                    toolBar.push({
                        type: 'button',
                        text: 'GIKAM.BUTTON.ASSIGN',
                        icon: 'modify',
                        onClick: function () {
                            var grid = Gikam.getComp(_this.idManage(step).sampleGridId)
                            var selections = grid.getSelections()
                            if (Gikam.isEmpty(selections)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                                return
                            }
                            let ids = selections.map(function (item) {
                                return item.id
                            }).join(',')
                            Gikam.create('modal', {
                                width: '90%',
                                height: '90%',
                                title: Gikam.choose.getConfig('sample-board-choose').title,
                                url: IFM_CONTEXT + Gikam.choose.getConfig("sample-board-choose").url + Gikam.param({
                                    id_IN: ids
                                }),
                                onAfterClose: function (rows) {
                                    grid.refresh()
                                }
                            })
                        }
                    })
                }
                toolBar.push({
                    type: 'form',
                    id: _this.idManage(step).sampleCodeFormId,
                    fields: [{
                        field: 'sampleCode',
                        title: 'T_LIMS_SAMPLE.SAMPLECODE',
                        onChange: function () {
                            _this.scanCodeCheck(id, step)
                        }
                    }]
                })
                columns = limsSampleHandle.getSampleListColumns(editor)
            } else {
                columns = limsSampleHandle.getSearchSampleListColumns()
            }
            return {
                type: 'tab',
                panels: [{
                    title: 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_INFO',
                    items: [{
                        type: 'grid',
                        id: _this.idManage(step).sampleGridId,
                        columns: columns,
                        onCellClick: function (field, row) {
                            field === 'ext$.batchcode' && limsSampleHandle.loadBatchDetailPage(row)
                        },
                        onRendered: function () {
                            if (step === 'edit' && pageType !== 'enter') {
                                this.hideColumns(['ext$.sampleconcentration_temp', 'ext$.samplevolume_temp', 'ext$.restcount_temp'])
                            }
                            if (step === 'search' && pageType !== 'enter') {
                                this.hideColumns(['sampleConcentration', 'sampleVolume', 'restCount'])
                            }
                        },
                        toolbar: toolBar,
                        url: limsSampleHandle.sampleUrl + '/queries/sampleHandles',
                        requestData: {
                            sampleHandleId_EQ: id
                        }
                    }]
                }, {
                    title: 'GIKAM.FILE.ATTACHMENT',
                    items: [{
                        type: 'uploader',
                        id: _this.idManage(step).uploaderId,
                        dbTable: 'T_LIMS_SAMPLE_HANDLE',
                        onBeforeUpload: function () {
                            if (!id || Gikam.isEmpty(id)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                                return false
                            }
                        },
                        formatterGridColumns: function (columns) {
                            return Gikam.getFormatterGridColumns(columns)
                        },
                        bizId: id,
                        readonly: !step || step !== 'edit',
                    }]
                }, {
                    title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                    items: [{
                        type: 'grid',
                        id: _this.idManage(step).flowGridId,
                        columns: limsSampleHandle.getFlowListColumns(),
                        url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_SAMPLE_HANDLE&id=' + id
                    }]
                }]
            }
        },

        scanCodeCheck: function (id, step) {
            var _this = this
            let formComp = Gikam.getComp(_this.idManage(step).sampleCodeFormId)
            var sampleCode = formComp.getData().sampleCode
            var url = limsSampleHandle.sampleUrl + '/scancodecheck'
            if (sampleCode) {
                if (id) {
                    Gikam.postText(url, Gikam.getJsonWrapper(null, [null, [{
                        sampleHandleId: id,
                        sampleCode: sampleCode
                    }]])).done(function (data) {
                        if (parseInt(data) === 1) {
                            Gikam.alert('SAMPLE.SCANCHECK.ALERT.DUPLICATE')
                        } else if (parseInt(data) === 2) {
                            Gikam.alert('SAMPLE.SCANCHECK.ALERT.FAIL')
                        } else if (parseInt(data) > 2) {
                            Gikam.getComp(_this.idManage(step).sampleGridId).refreshRowById(parseInt(data))
                        }
                        formComp.cleanData().done(function () {
                            formComp.setData({sampleCode: ''})
                        })
                        formComp.getField('sampleCode').focus()
                    })
                }
            }
        },

        idManage: function (step) {
            return {
                sampleGridId: pageType + 'lims-handle-sample-list-grid' + step,
                uploaderId: pageType + 'sample-handle-uploader' + step,
                flowGridId: pageType + 'handle-flow-edit-list-grid' + step,
                packFormId: pageType + 'package-form-obj' + step,
                sampleCodeFormId: pageType + 'sampleCode-form-boj' + step,
            }
        },

        createPage: function () {
            let _this = this
            Gikam.create('layout', {
                center: {
                    items: [this.getBtnToolbar(_this.param.id, _this.param.step), this.getSampleHandleInfo(_this.param.id, _this.param.step)]
                },
                toolbarAlign: 'right',
                renderTo: workspace.window.$dom
            })
            return this
        },

        init: function (param) {
            this.param = param
            this.createPage()
        }
    }
}
